package com.atguigu.gmall.interceptor;

import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.common.util.HttpUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * @author lfy
 * @Description
 * @create 2023-07-29 15:30
 */
@Slf4j
@Component
public class UserIdHeaderRequestInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
//        HttpServletRequest request = CartController.requestMap.get(Thread.currentThread());
//        HttpServletRequest request = CartController.threadLocal.get();
//        log.info("拦截器...");
//        String userid = request.getHeader("userid");

        //终极进化
        //1、得到老请求
        HttpServletRequest request = HttpUtil.currentRequest();
        if(request != null){
            String userid = request.getHeader(RedisConst.USER_ID_HEADER);
            if(StringUtils.hasText(userid)){
                template.header(RedisConst.USER_ID_HEADER,userid);
            }
            String usertempid = request.getHeader(RedisConst.USER_TEMPID_HEADER);
            if(StringUtils.hasText(usertempid)){
                template.header(RedisConst.USER_TEMPID_HEADER,usertempid);
            }

        }

    }
}
